package com.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pinda.dao.AddressToCoordinate;
import com.pinda.dao.distanceUiils.distanceUtils;
import com.pinda.dao.vo.ReturnLocationBean;
import com.pinda.feign.UpdateStatusFeign;
import com.pinda.oms.entity.PdOrder;
import com.pinda.mapper.PdOrderMapper;
import com.pinda.service.IPdOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@Service
public class PdOrderServiceImpl extends ServiceImpl<PdOrderMapper, PdOrder> implements IPdOrderService {

    @Resource
    PdOrderMapper pdOrderMapper;

    @Resource
    private UpdateStatusFeign updateStatusFeign;




    /**
     * 根据订单ID修改订单状态未运输中
     * @param pdOrder
     * @return
     */
    @Override
    public Boolean orderUpdateByOrderId(PdOrder pdOrder) {
//        QueryWrapper<PdOrder> queryWrapper =new QueryWrapper<>();
//        queryWrapper.eq("id",orderId);
        int update = pdOrderMapper.updateById(pdOrder);
        return update!=0;
    }

    @Override
    public PdOrder saveOrder(PdOrder pdOrder) throws Exception {

        distanceUtils distanceUtils = new distanceUtils();
        AddressToCoordinate addressToCoordinate = new AddressToCoordinate();

        ReturnLocationBean returnLocationBean1 = addressToCoordinate.getReturnLocationBean(pdOrder.getSenderAddress());
        ReturnLocationBean returnLocationBean2 = addressToCoordinate.getReturnLocationBean(pdOrder.getReceiverAddress());


        double distance = distanceUtils.getDistance(returnLocationBean1.getResult().getLocation().getLng(), returnLocationBean1.getResult().getLocation().getLat(), returnLocationBean2.getResult().getLocation().getLng(), returnLocationBean2.getResult().getLocation().getLat());
        pdOrder.setDistance(BigDecimal.valueOf(distance));
        int insert = this.pdOrderMapper.insert(pdOrder);
        return pdOrder;
    }



    /**
     * 修改收件状态为3并且修改当前订单的金额(运费)
     * @param order
     * @param id
     * @return
     */
    @Override
    public Boolean updataAmountByid(PdOrder order,String id) {
        Boolean updatareceiving = updateStatusFeign.updatareceivingByid3(id);
        if (updatareceiving){
            return pdOrderMapper.updataAmountByid(order);
        }
        return false;
    }


}
